//
//  ServiceCostView.m
//  51易借
//
//  Created by 张睿 on 2017/8/30.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "ServiceCostView.h"

@interface ServiceCostView ()

@property (nonatomic, weak)UIImageView * backImageView;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, strong)UILabel * appCostLabel;
@property (nonatomic, strong)UILabel * appCostValueLabel;
@property (nonatomic, strong)UILabel * messageLabel;
@property (nonatomic, strong)UILabel * messageValueLabel;
@property (nonatomic, strong)UILabel * serviceLabel;
@property (nonatomic, strong)UILabel * serviceValueLabel;
@property (nonatomic, strong)UILabel * readyLabel;
@property (nonatomic, strong)UILabel * readyValueLabel;
@property (nonatomic, weak)UILabel * explationLabel;
@property (nonatomic, weak)UIButton * cancelButton;
@property (nonatomic, weak)UIButton * sureButton;

@end

@implementation ServiceCostView

//平台服务费
- (void)setPlatformServiceFee:(NSString *)platformServiceFee{
    
    _platformServiceFee = platformServiceFee;
    
//    float platform = [platformServiceFee floatValue];
    
    self.appCostValueLabel.text = [NSString stringWithFormat:@"%.2f元",[platformServiceFee floatValue]];
}
//信息认证费用
- (void)setInfoAuthFee:(NSString *)infoAuthFee{
    
    _infoAuthFee = infoAuthFee;
    
    self.messageValueLabel.text = [NSString stringWithFormat:@"%.2f元",[infoAuthFee floatValue]];
}
//风控服务费
- (void)setRiskServiceFee:(NSString *)riskServiceFee{
    
    _riskServiceFee = riskServiceFee;
    
//    self.serviceValueLabel.text = riskServiceFee;
    self.serviceValueLabel.text = [NSString stringWithFormat:@"%.2f元",[riskServiceFee floatValue]];
}
//风控准备金
- (void)setRiskPrepareFee:(NSString *)riskPrepareFee{
    
    _riskPrepareFee = riskPrepareFee;
    
//    self.readyValueLabel.text = riskPrepareFee;
    self.readyValueLabel.text = [NSString stringWithFormat:@"%.2f元",[riskPrepareFee floatValue]];
}

- (void)setExplationString:(NSString *)explationString{
    
    _explationString = explationString;
    
//    self.explationLabel.text = @"如您未及时还款，将会产生逾期滞纳金，计算标准为25元/天/100元";
    self.explationLabel.text = [NSString stringWithFormat:@"如您未及时还款，将会产生逾期滞纳金，计算标准为%@元/天/100元",explationString];
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {

        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    
    return self;
}



- (void)setupUI{
    
    UIImageView * backImageView = [UIImageView new];
    
    backImageView.image = [UIImage imageNamed:@"服务费用弹框"];
    
    self.backImageView = backImageView;
    
    [self addSubview:self.backImageView];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = [UIColor whiteColor];
//    titleLabel.font = [UIFont fontWithName:TYPE_FACE size:16 * SCREENH6];
    titleLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"服务费用";
    
    self.titleLabel = titleLabel;
    
    [self.backImageView addSubview:self.titleLabel];
    
    
    self.appCostLabel = [self setupLabelWithString:@"平台服务费"];
    
    [self addSubview:self.appCostLabel];
    
    self.appCostValueLabel = [self setupLabelWithString:@"13元"];
    
    [self addSubview:self.appCostValueLabel];
    
    self.messageLabel = [self setupLabelWithString:@"信息认证费"];
    
    [self addSubview:self.messageLabel];
    
    self.messageValueLabel = [self setupLabelWithString:@"10元"];
    
    [self addSubview:self.messageValueLabel];
    
    self.serviceLabel = [self setupLabelWithString:@"风险服务费"];
    
    [self addSubview:self.serviceLabel];
    
    self.serviceValueLabel = [self setupLabelWithString:@"20元"];
    
    [self addSubview:self.serviceValueLabel];
    
    self.readyLabel = [self setupLabelWithString:@"风险准备金"];
    
    [self addSubview:self.readyLabel];
    
    self.readyValueLabel = [self setupLabelWithString:@"100元"];
    
    [self addSubview:self.readyValueLabel];
    
    
    UILabel * explationLabel = [UILabel new];
    
    explationLabel.text = @"如您未及时还款，将会产生逾期滞纳金，计算标准为25元/天/100元";
    explationLabel.numberOfLines = 0;
    explationLabel.font = [UIFont systemFontOfSize:9 * SCREENH6];
    explationLabel.textColor = [UIColor lightGrayColor];
    explationLabel.textAlignment = NSTextAlignmentCenter;
    
    self.explationLabel = explationLabel;
    
    [self addSubview:self.explationLabel];
    
    
    UIButton * cancelButton = [UIButton new];
    
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(didHiddenButton) forControlEvents:UIControlEventTouchUpInside];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.cancelButton = cancelButton;
    
    [self addSubview:self.cancelButton];
    
    
    UIButton * sureButton = [UIButton new];
    
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:TOTAL_COLOR forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(didHiddenButton) forControlEvents:UIControlEventTouchUpInside];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.sureButton = sureButton;
    
    [self addSubview:self.sureButton];
}



- (void)layoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self).offset(7 * SCREENH6);
    }];

    [self.appCostLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(58 * SCREENH6);
        make.top.mas_equalTo(self).offset(50 * SCREENH6);
    }];

    [self.appCostValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self).offset(-72 * SCREENH6);
        make.centerY.mas_equalTo(self.appCostLabel);
    }];
    
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.appCostLabel);
        make.top.mas_equalTo(self.appCostLabel.mas_bottom).offset(18 * SCREENH6);
    }];
    
    [self.messageValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.appCostValueLabel);
        make.centerY.mas_equalTo(self.messageLabel);
    }];
    
    [self.serviceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.messageLabel);
        make.top.mas_equalTo(self.messageLabel.mas_bottom).offset(18 * SCREENH6);
    }];
    
    [self.serviceValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.messageValueLabel);
        make.centerY.mas_equalTo(self.serviceLabel);
    }];
    
    [self.readyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.serviceLabel);
        make.top.mas_equalTo(self.serviceLabel.mas_bottom).offset(18 * SCREENH6);
    }];
    
    [self.readyValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.serviceValueLabel);
        make.centerY.mas_equalTo(self.readyLabel);
    }];
    
    [self.explationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.readyLabel.mas_leading).offset(-6 * SCREENH6);
        make.trailing.mas_equalTo(self.readyValueLabel.mas_trailing).offset(8 * SCREENH6);
        make.top.mas_equalTo(self.readyLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(25 * SCREENH6);
        make.width.mas_equalTo(100 * SCREENH6);
        make.height.mas_equalTo(35 * SCREENH6);
        make.bottom.mas_equalTo(-5 * SCREENH6);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self).offset(-25 * SCREENH6);
        make.width.height.centerY.mas_equalTo(self.cancelButton);
    }];
}


- (UILabel *)setupLabelWithString:(NSString *)string{
    
    UILabel * label = [UILabel new];
    
    label.textColor = [UIColor blackColor];
//    label.font = [UIFont fontWithName:TYPE_FACE size:15 * SCREENH6];
    label.font = [UIFont systemFontOfSize:15 * SCREENH6];
    label.textAlignment = NSTextAlignmentRight;
    label.text = string;
    
    return label;
}


- (void)didHiddenButton{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hiddenView" object:nil];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


-(void)setModel:(BorrowDetailModel *)model
{
    _model = model;
    self.appCostValueLabel.text = [NSString stringWithFormat:@"%.2f元", [self.model.platformServiceFee floatValue]];
    
    self.messageValueLabel.text = [NSString stringWithFormat:@"%.2f元", [self.model.infoAuthFee floatValue]];
    self.serviceValueLabel.text = [NSString stringWithFormat:@"%.2f元", [self.model.riskServiceFee floatValue]];
//    self.explationLabel.text = [NSString stringWithFormat:@"%.2f元", [self.model.riskServiceFee floatValue]];
    self.readyValueLabel.text = [NSString stringWithFormat:@"%.2f元", [self.model.riskPrepareFee floatValue]];

    float borrowAmount = [model.borrowAmount floatValue];
    float overdueRate = [model.overdueRate floatValue];
    float explation = borrowAmount * overdueRate;
    
    self.explationLabel.text = [NSString stringWithFormat:@"如您未及时还款，将会产生逾期滞纳金，计算标准为%.2f元/天/100元",explation];
}



@end
